<script lang="ts" setup>
import { useRoute } from "vue-router";
import {onMounted, onUnmounted, reactive, ref} from "vue";
import indexText from "./indexText.json"
import { requestSongListHotCom, requestSongAllComment } from "../../../utils/axiosURL"

const router = useRoute()
// 歌单ID
const songListID = ref<any>(router.query.id)
// emoji显示状态
let emojiShowState = ref<boolean>(false)
// emoji容器实例
const emojiExample = ref<any>(null)
// 文本域内容
let ctnTextArea = ref<string>('')
// 推荐文案实例
const copyWritingExample = ref<any>(null)
// 推荐文案容器实例
let copyWritingState = ref<any>(false)
// prompt 组件显示状态
let promptState = ref<any>(false)
// 清除定时器
let timer = ref<any>(null)
// 存储热门评论
const saveHotCommentData = reactive<any>([])
// 热门评论总数
const hotCommentTotal = ref<any>(null)
// 请求歌单所有评论的页码
let allCommentPage = ref<any>(1)
// 存储所有歌单评论
let saveAllCommentData = reactive<any>([])
// 所有评论的长度
const allCommentLength = ref<any>(0)

// 请求歌单的热门评论
const requestSongListHotComFn = () => {
  requestSongListHotCom(songListID.value, 0, 2).then((data: any) => {
    saveHotCommentData.values = data.hotComments
    hotCommentTotal.value = data.total
  }).catch(err => {
    console.log(err)
  })
}
// 请求歌单所有评论
const requestSongAllCommentFn = () => {
  requestSongAllComment(songListID.value, allCommentPage.value).then((data: any) => {
    saveAllCommentData.values = data.comments
    allCommentLength.value = data.total
  }).catch(err => {
    console.log(err)
  })
}

// 切换emoji容器的显示状态
const changeEmojiFn = () => {
  copyWritingState.value = false
  emojiShowState.value = !emojiShowState.value
}
// 拿取emoji
const getEmoji = (index: number) => ctnTextArea.value += emojiExample.value.children[index].innerHTML
// 文案点击事件函数
const copyWritingFn = (index: number) => ctnTextArea.value += copyWritingExample.value.children[index].innerHTML
// 切换推荐文案容器状态
const changeCopyWritingFn = () => {
  emojiShowState.value = false
  copyWritingState.value = !copyWritingState.value
}
// 提交评论事件处理函数
const submitCommentFn = () => {
  if (ctnTextArea.value === '') {
    promptState.value = true
    clearTimeout(timer)
    timer = setTimeout(() => {promptState.value = false}, 1500)
  }
}

const changePages = () => {
  requestSongAllCommentFn()
}
onMounted(() => {
  requestSongListHotComFn()
  requestSongAllCommentFn()
})
// 监听页面点击事件隐藏emoji容器
window.addEventListener('click', () => emojiShowState.value = copyWritingState.value = false)
onUnmounted(() => {
  // 销毁事件
  window.removeEventListener('click', () => {})
})

</script>


<template>
  <div class="commentArea">
    <div class="commentWrap">
      <div class="textArea">
        <textarea v-model="ctnTextArea" maxlength="140"></textarea>
      </div>
      <div class="moreWrap">
        <div class="leftWrap">
          <span class="iconfont icon-smile" :class="`${emojiShowState ? 'activeColor' : ''}`" @click.stop="changeEmojiFn"></span>
          <span class="iconfont icon-aite"></span>
          <span class="iconfont icon-hash" :class="`${copyWritingState ? 'activeColor' : ''}`" @click.stop="changeCopyWritingFn"></span>
        </div>
        <div class="rightWrap">
          <button @click="submitCommentFn">评论</button>
        </div>
        <div v-if="emojiShowState" class="emoji" ref="emojiExample">
          <span v-for="(item, index) in indexText.emoji" @click.stop="getEmoji(index)">{{ item }}</span>
        </div>
        <div class="recommendedCopy" v-if="copyWritingState">
          <p class="RecommendForYou">为你推荐</p>
          <ul ref="copyWritingExample">
            <li v-for="(item, index) in indexText.recommendCopy" @click="copyWritingFn(index)">{{ item }}</li>
          </ul>
        </div>
      </div>
    </div>
    <div class="hotCommentList">
      <p class="excellentComment">精彩评论</p>
      <commentItem v-if="saveHotCommentData.values.length" :commentData=saveHotCommentData.values />
      <commentItemSkeleton v-else v-for="item in 10" />
      <div v-if="hotCommentTotal > 10" class="viewMore">
        <router-link :to="{path: '/MoreHotComment', query: {id: songListID}}">
          <button>
            更多精彩评论
            <span class="iconfont icon-xiangyoujiantou"></span>
          </button>
        </router-link>
      </div>
    </div>
    <div class="allCommentList">
      <p class="allComTitle">全部评论</p>
      <commentItem v-if="saveAllCommentData.values.length" :commentData=saveAllCommentData.values />
      <commentItemSkeleton v-else v-for="item in 10" />
      <div class="paging">
        <el-pagination
          small
          background
          layout="prev, pager, next"
          :total="allCommentLength"
          v-model:current-page="allCommentPage"
          @current-change="changePages"
        />
      </div>
    </div>
    <prompt v-if="promptState" :content=indexText.errorText :type=indexText.errorType />
  </div>
</template>


<style lang="scss" scoped>
.commentArea {
  width: 100%;
  padding: 0 30px 30px;
  box-sizing: border-box;
  margin-top: 20px;
  .commentWrap {
    width: 100%;
    height: 120px;
    .textArea {
      position: relative;
      width: 100%;
      height: 80px;
      textarea {
        width: 100% !important;
        height: 100% !important;
        padding: 4px 8px;
        outline: none;
        border: 1.5px solid $lineColor;
        box-sizing: border-box;
        border-radius: $borderRadius8;
        font-size: $font-size-xs;
        font-family: $textFont;
      }
      &::before {
        content: "140";
        position: absolute;
        right: 3px;
        bottom: 3px;
        width: 20px;
        font-size: $font-size-xs;
        font-family: $textFont;
        color: $font_second_color;
        background: #fff;
      }
    }
    .moreWrap {
      position: relative;
      display: flex;
      width: 100%;
      height: 40px;
      .leftWrap {
        flex-basis: 90px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        span {
          color: $font_second_color;
          font-size: $font-size-lg;
          &:hover {
            color: $font_main_color;
          }
        }
        .activeColor {
          color: $font_second_color !important;
        }
      }
      .rightWrap {
        flex: 1;
        display: flex;
        justify-content: end;
        align-items: center;
        button {
          @extend .clearButton;
          width: 60px;
          height: 30px;
          border-radius: 25px;
          border: 1px solid $lineColor;
        }
      }
      .emoji {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        position: absolute;
        top: 40px;
        left: 0;
        width: 375px;
        padding: 8px;
        box-sizing: border-box;
        box-shadow: 0px 0px 8px 0px rgb(230,230,230);
        border-radius: 6px;
        background-color: #fff;
        span {
          margin: 0 10px 10px 0;
        }
        &::after {
          content: "×";
          position: absolute;
          top: 0;
          right: 0;
          width: 20px;
          height: 20px;
          text-align: center;
          color: $font_second_color;
        }
      }
      .recommendedCopy {
        position: absolute;
        top: 40px;
        left: 72px;
        width: 285px;
        box-shadow: 0px 0px 8px 0px rgb(230,230,230);
        border-radius: 6px;
        background-color: #fff;
        .RecommendForYou {
          color: $font_second_color;
          font-size: $font-size-sm;
          padding-left: 15px;
          box-sizing: border-box;
          line-height: 35px;
          box-shadow: 0px 1px 2px 0 rgb(230,230,230);
        }
        ul {
          li {
            color: $font_second_color;
            font-size: $font-size-sm;
            padding-left: 15px;
            box-sizing: border-box;
            line-height: 35px;
            &:nth-child(1) {
              background-color: rgb(250,250,250);
            }
            &:hover {
              background-color: rgb(250,250,250);
            }
          }
        }
      }
    }
  }
  .hotCommentList {
    width: 100%;
    margin-top: 10px;
    .excellentComment {
      font-family: $activeTextFont;
    }
    .viewMore {
      display: flex;
      justify-content: center;
      margin-top: 40px;
      button {
        @extend .clearButton;
        border: 1px solid $lineColor;
        padding: 6px 20px;
        border-radius: 25px;
        cursor: pointer;
        font-size: $font-size-sm;
        span {
          vertical-align: -1px;
          font-size: $font-size-sm;
        }
      }
    }
  }
  .allCommentList {
    width: 100%;
    margin-top: 50px;
    .allComTitle {
      font-family: $activeTextFont;
    }
    .paging {
      display: flex;
      justify-content: center;
      width: 100%;
      margin-top: 50px;
      :deep(.number) {
        border-radius: 4px;
        font-family: none;
        font-family: $textFont;
        background-color: transparent;
        border: 1px solid $lineColor;
        &:hover {
          color: $font_main_color;
          background-color: $BK_GRAY;
        }
      }
      :deep(.is-active) {
        background-color: $theme-red;
        border-radius: 4px;
        &:hover {
          color: $theme-white;
          background-color: $theme-red;
        }
      }
      :deep(.btn-prev),
      :deep(.btn-next)
      {
        border-radius: 4px;
        border: 1px solid $lineColor;
        background-color: transparent;
        &:hover {
          color: $font_red_color;
          background-color: $BK_GRAY;
        }
      }
      :deep(.more) {
        border-radius: 4px;
        border: 1px solid $lineColor;
        background-color: transparent;
        &:hover {
          color: $font_red_color;
          background-color: $BK_GRAY;
        }
      }
    }
  }
}

</style>
